<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="关键字" prop="searchParam">
        <el-input
            v-model="queryParams.searchParam"
            placeholder="请输入用户ID、手机号搜索"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源渠道" prop="source">
        <el-select
          v-model="queryParams.source"
          placeholder="来源渠道"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.channel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select
            v-model="queryParams.userType"
            placeholder="用户类型"
            clearable
            size="small"
            style="width: 240px"
        >
          <el-option
              v-for="dict in dict.type.app_user_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登录方式" prop="loginType">
        <el-select
          v-model="queryParams.loginType"
          placeholder="登录方式"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.login_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" align="center" width="120" sortable />
      <el-table-column label="用户ID" align="center" prop="userId" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="用户名" align="center" prop="username" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="注册日期" align="center" prop="registerDate" width="160" />
      <el-table-column label="来源渠道" prop="source" align="center" width="100"/>
      <el-table-column label="用户类型" align="center" prop="userType" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.userType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员时间" align="center" prop="memberDate" width="300">
        <template slot-scope="scope">
          <span>{{ scope.row.memberDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录方式" align="center" prop="loginType" width="120"/>
      <el-table-column label="最后登录" align="center" prop="lastLoginDate" />
      <el-table-column label="操作" align="center" width="200" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
              size="mini"
              type="text"
              style="color: #f00"
              @click="handleFreeze(scope.row)"
          >{{ scope.row.status === 1 ? '冻结' : '取消冻结' }}</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleCancel(scope.row)"
            style="color: #f00"
            v-if="scope.row.userTypeCode === 2"
          >取消会员</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #f00"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script>
import { getAppUserList, freezeAppUser, cancelVipUser, removeAppUser } from "@/api/shuiyin/shuiyin.js";

export default {
  name: "AppUser",
  dicts: ['app_user_type', 'login_type', 'channel'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchParam: undefined,
        loginType: undefined,
        source: undefined,
        userType: undefined
      },
      // 来源渠道
      channelData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      getAppUserList(this.queryParams).then(
        response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const userId = row.id;
      this.$router.push("/userDetail/user/" + userId);
    },
    /** 冻结和取消冻结操作 */
    handleFreeze(row) {
      const userId = row.id;
      freezeAppUser(userId).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
    /** 取消会员 */
    handleCancel(row) {
      const userId = row.id;
      cancelVipUser(userId).then(response => {
        this.$modal.msgSuccess("取消成功");
        this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + userIds + '"的数据项？').then(function() {
        return removeAppUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
